package com.pengcheng.taskapplication.mvp.view.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.DeviceStatusModel;
import com.pengcheng.taskapplication.model.WashOrderModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenterV2;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.CheckDeviceStatusUtil;
import com.pengcheng.taskapplication.util.SPUtils;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;


public class ParkWashStatusV2 extends BaseActivity implements IView {
    private UserPresenterV2 userPresenter;
    private String uid;
    private String newtoken;

    @BindView(R.id.tv_show_tip)
    TextView tv_show_tip;

    @BindView(R.id.tv_show_device_status)
    TextView tv_show_device_status;

    @BindView(R.id.img_status)
    ImageView img_status;


    private String deviceid="";
    private String tradno="";

    private boolean manuStop;
    private boolean onWorking;

    private final Timer timer = new Timer();
    private TimerTask task;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            // 要做的事情
            super.handleMessage(msg);
            //getWashOrderDetail(newtoken,uid,tradno);
            geDeviceStatus(newtoken,uid,deviceid);
        }
    };

    @Override
    protected int layoutResID() {
        return R.layout.activity_wash_status_layout_v2;
    }


    @Override
    protected void initData() {

        tradno=getIntent().getStringExtra("tradno");
        deviceid=getIntent().getStringExtra("deviceid");
        userPresenter = new UserPresenterV2(this);
        uid= SPUtils.getString(context,ConstantValue.SpType.userid);
        newtoken=SPUtils.getString(context,ConstantValue.SpType.newtoken);
        if(tradno!=null&&tradno.length()!=0) {
            task = new TimerTask() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    Message message = new Message();
                    message.what = 1;
                    handler.sendMessage(message);
                }
            };
            timer.schedule(task, 0, 2000);
        }


    }

    /**
     * 1、车没有停到位的文案：车辆没有停到位，请按照指示灯要求停车入位
     * 2、停车到位之后的文案：车辆已到位，请选择洗车模式开始洗车
     * 3、非车牌用户选择模式文案：暂时不需要增加
     * 4、等待洗车文案：车辆正在清洁中，请等待
     * 5、非自己洗车中文案：已有车辆在洗车，请等待
     *
     * 如果currenttradno有值  说明当前设备正在洗这个订单      如果nostarttradno有值当前用户在该设备已支付但未开始的订单号
     * @param code
     * @param msg
     * @param data
     * @param actionType
     */

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.getDeviceStatus)){
            DeviceStatusModel deviceStatusModel=  (DeviceStatusModel)data;
            String carready=deviceStatusModel.getCarready();
            String washing=deviceStatusModel.getWashing();
            String washdong=deviceStatusModel.getWashdone();
            if(carready.equals("0")&&washing.equals("0")&&washdong.equals("0")){
                //当前设备空载
            }
            if(carready.equals("1")){
                //停好车了

            }else {
                //车辆未停好车
                tv_show_tip.setText("车辆没有停到位，请按照指示灯要求停车入位");
                img_status.setImageResource(R.drawable.car_no_ready_icon);
                tv_show_device_status.setVisibility(View.GONE);
            }
            if(carready.equals("0")&&washing.equals("1")&&washdong.equals("0")){
                //洗车中
                tv_show_tip.setText("车辆正在清洁中");
                img_status.setImageResource(R.drawable.washing_icon);
                tv_show_device_status.setVisibility(View.VISIBLE);
                tv_show_device_status.setText("手动中止");
            }
            if(carready.equals("0")&&washing.equals("0")&&washdong.equals("1")){
                //洗车完成
                tv_show_tip.setText("车辆已经清洁完毕");
                img_status.setImageResource(R.drawable.wash_done_icon);
                tv_show_device_status.setVisibility(View.VISIBLE);
                tv_show_device_status.setText("已完成");
                task.cancel();
            }


        }


        if(actionType.equals(ConstantValue.RequestKey.stopDevice)){
            //手动关闭过

        }
        if(actionType.equals(ConstantValue.RequestKey.getOrderDetail)){
            WashOrderModel washOrderModel=(WashOrderModel)data;
            geDeviceStatus(newtoken,uid,washOrderModel.getDeviceid());
        }
    }

    @Override
    public void onFailed(String code, String msg) {

    }

    public void geDeviceStatus(String token,String uid,String deviceid){
        userPresenter.getStatus(token,uid,deviceid, ConstantValue.RequestKey.getDeviceStatus);
    }

    public void startDevice(String token,String uid,String tradno){
        userPresenter.startDevice(token,uid,tradno, ConstantValue.RequestKey.startDevice);
    }

    public void stopDevice(String token,String uid,String tradno){
        userPresenter.stopDevice(token,uid,tradno, ConstantValue.RequestKey.stopDevice);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(timer!=null){
            timer.cancel();
        }
    }
    @OnClick({R.id.img_back,R.id.tv_show_device_status,R.id.ll_scan})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_show_device_status:
                 if(tv_show_device_status.getText().toString().equals("手动中止")){
                     stopDevice(newtoken,uid,tradno);
                 }else if (tv_show_device_status.getText().toString().equals("已完成")){
                    ParkWashStatusV2.this.finish();
                 }
                break;
        }
    }

    public  void getWashOrderDetail(String token,String uid,String tradno){
        userPresenter.getWashOrderDetail(token,uid,tradno,ConstantValue.RequestKey.getOrderDetail);
    }


}
